//
//  ViewController.swift
//  AnimationGuard
//
//  Created by onemade on 11/4/15.
//  Copyright © 2015 CNPC. All rights reserved.
//

import UIKit

class ViewController: UIViewController,HolderViewDelegate {
    
    var holderView = HolderView(frame: CGRectZero)

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        addHolderView()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func animateLabel() {
        holderView.removeFromSuperview()
        view.backgroundColor = Colors.blue
        
        let label: UILabel = UILabel(frame: view.frame)
        label.textColor = Colors.white
        label.font = UIFont(name: "HelveticaNeue-Thin", size: 170.0)
        label.textAlignment = NSTextAlignment.Center
        label.text = "S"
        label.transform = CGAffineTransformScale(label.transform, 0.25, 0.25)
        view.addSubview(label)
        
        UIView.animateWithDuration(0.4, delay: 0.0, usingSpringWithDamping: 70, initialSpringVelocity: 0.1, options: .CurveEaseInOut, animations: {
                label.transform = CGAffineTransformScale(label.transform, 4.0, 4.0)
            }, completion: {finished in
                self.addButton()
        })
    }
    
    func addHolderView(){
        let box : CGFloat  = 100.0
        holderView.frame = CGRect(x: view.bounds.width/2 - box/2, y: view.bounds.height/2 - box/2, width: box, height: box)
        holderView.parentFrame = view.frame
        holderView.delegate = self
        view.addSubview(holderView)
        holderView.addOval()
    }
    
    func addButton(){
        let button = UIButton()
        button.frame = CGRect(x: 0.0, y: 0.0, width: view.bounds.width, height: view.bounds.height)
        button.addTarget(self, action: Selector("buttonPressed:"), forControlEvents: .TouchUpInside )
        view.addSubview(button)
    }
    
    func buttonPressed(sender:UIButton){
        view.backgroundColor = Colors.white
        view.subviews.map({$0.removeFromSuperview()})
        
        holderView = HolderView(frame: CGRectZero)
        addHolderView()
    }
}

